<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Deo
 * Date: 10/23/13
 * Time: 5:32 PM
 * To change this template use File | Settings | File Templates.
 */

class Information_Topics_Model extends CI_Model{


    public function getTopics()
    {
        $sql = "SELECT * FROM exp_fmis_information_topics";
        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    public function getTopicById($topicId)
    {
        $sql = "SELECT * FROM exp_fmis_information_topics WHERE topic_id LIKE ?";
        $query =  $this->db->query($sql, $topicId);
        $result = $query->row_array();
        return $result;
    }

    public function addTopic($topicName, $topicContent)
    {
        $id = $this->getMaxTopicId() + 1;

        $sql = "INSERT INTO exp_fmis_information_topics(`topic_name`, `topic_content`, `topic_id`)
                VALUES (?, ?, ?);";
        $this->db->query($sql, array($topicName, $topicContent, $id));
    }

    public function removeTopic($topicName)
    {
        //TODO
    }

    public function updateTopic($topicId, $topicContent)
    {
        $sql = "UPDATE exp_fmis_information_topics SET topic_content = ? WHERE topic_id = ? ";
        $this->db->query($sql, array($topicContent, $topicId));
    }

    private function getMaxTopicId()
    {
        $sql = "SELECT MAX(topic_id) as 'maxid' FROM exp_fmis_information_topics";
        $query =  $this->db->query($sql);
        $row = $query->row();
        return $row->maxid;
    }
}